//
//  FPPeriodMainView.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/23.
//

#import "FPPeriodMainView.h"
#import "FPPeriodMainItem.h"

@interface FPPeriodMainView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataArray; // 数据源

@end

@implementation FPPeriodMainView

- (void)setIndex:(NSInteger)index {
    
    _index = index;
    [self.collectionView reloadData];
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    self.titleLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    
    self.confirmBtn.clipsToBounds = YES;
    self.confirmBtn.layer.cornerRadius = 22.f;
    self.confirmBtn.backgroundColor = MAIN_BLUE_COLOR;
    
    // 初始化数据
//    self.dataArray = @[@"3期", @"6期", @"9期", @"12期", @"24期"]; 112.31
    self.dataArray = @[@"12期", @"9期" , @"6期", @"3期"];
  
    // 设置布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 横向滚动
    
    // 创建 UICollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.showsHorizontalScrollIndicator = NO; // 隐藏滚动条
    
    // 注册 Cell
    [self.collectionView registerClass:[FPPeriodMainItem class] forCellWithReuseIdentifier:@"Cell"];
    
    // 添加到视图
    [self.cellContentView addSubview:self.collectionView];
    
    // 设置约束
    self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.collectionView.leadingAnchor constraintEqualToAnchor:self.cellContentView.leadingAnchor constant:24],
        [self.collectionView.trailingAnchor constraintEqualToAnchor:self.cellContentView.trailingAnchor constant:-24],
        [self.collectionView.centerYAnchor constraintEqualToAnchor:self.cellContentView.centerYAnchor],
        [self.collectionView.heightAnchor constraintEqualToConstant:73.f] // 高度可根据需要调整
    ]];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FPPeriodMainItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    cell.clipsToBounds = YES;
    cell.layer.cornerRadius = 8.f;
    cell.backgroundColor = [UIColor hexColor:@"#F3F5F9"];
    cell.layer.borderWidth = 1.f;
    cell.layer.borderColor = [UIColor hexColor:@"#E1E3E7"].CGColor;
    cell.titleLabel.textColor = [UIColor hexColor:@"#3A3A3A"];
    
    [cell configureWithText:self.dataArray[indexPath.row]];
    
    if (self.index == indexPath.row) {
        
        cell.backgroundColor = [UIColor hexColor:@"#F2F7FF"];
        cell.layer.borderColor = MAIN_BLUE_COLOR.CGColor;
        cell.titleLabel.textColor = MAIN_BLUE_COLOR;
    }
    
    
    return cell;
}

#pragma mark - UICollectionView DelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 计算单个内容的宽度
    CGFloat collectionViewWidth = SCREEN_WIDTH - 40.f;
    CGFloat totalSpacing = 12 * (4 - 1); // 总间距
    CGFloat availableWidth = collectionViewWidth - totalSpacing; // 可用宽度
    CGFloat itemWidth = availableWidth / 4 - 10; // 平分宽度
    return CGSizeMake(itemWidth, collectionView.bounds.size.height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 12; // 设置内容之间的间距
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    self.index = indexPath.row;
//    [self.collectionView reloadData];
}


- (IBAction)confirmAction:(UIButton *)sender {
    
    self.transmitBlock(self.index , self.dataArray[self.index]);
}


@end
